package
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.ui.MouseCursor;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	public class atestat extends MovieClip
	{
		var noduri:Array=new Array();
		var nrNoduri:int = 10;
		var nrMaxim=40;
		
		private var n1 : nod;
		private var n2 : nod;
		private var flag : Boolean = false;
		
		private var adauga : buton=new buton();
		private var nrNoduriText : campText=new campText(TextFieldType.INPUT);
		
		private var bfButon : buton=new buton();
		private var dfButon : buton=new buton();
		private var nodDf : campText=new campText(TextFieldType.INPUT);
		private var defaultNod : int = 1;
		
		private var arataLogo : buton=new buton();
		private var logg : logo;
		
		private var bGround : backGround = new backGround();
		
		public function atestat()
		{
			addEventListener(Event.ADDED_TO_STAGE,initializare);
		}
		
		private function initializare(e:Event)
		{	
			this.addChild(bGround);
			
			nrNoduriText.x=10;
			nrNoduriText.y=11;
			
			nrNoduriText.text=nrNoduri+"";
			this.addChild(nrNoduriText);
			
			adauga.x=nrNoduriText.x+nrNoduriText.width+10;
			adauga.y=10;
			
			adauga.nume="Adauga";
			
			this.addChild(adauga);
			
			adauga.addEventListener(MouseEvent.CLICK,getNrNoduri);
			
			//--------
			nodDf.x=adauga.x+adauga.width+40;
			nodDf.y=11;
			
			nodDf.text=defaultNod+"";
			this.addChild(nodDf);
			
			dfButon.x=nodDf.x+nodDf.width+10;
			dfButon.y=10;
			
			
			dfButon.width+=18;
			dfButon.nume="Depth First";
			
			this.addChild(dfButon);
			
			dfButon.addEventListener(MouseEvent.CLICK,getNodDf);
			
			bfButon.x=dfButon.x+dfButon.width+5;
			bfButon.y=10;
			
			bfButon.width+=27;
			bfButon.nume="Breadth First";
			
			this.addChild(bfButon);
			
			bfButon.addEventListener(MouseEvent.CLICK,getNodBf);
			
			dfButon.mouseEnabled=false;
			bfButon.mouseEnabled=false;
			nodDf.mouseEnabled=false;
			//--------
			arataLogo.x=stage.stageWidth-arataLogo.width-20;
			arataLogo.y=10;
			arataLogo.width+=10;
			arataLogo.nume="Informatii";
			
			this.addChild(arataLogo);
			
			arataLogo.addEventListener(MouseEvent.CLICK,afiseazaLogo);
		}
		
		private function adaugaNoduri():void
		{
			var i:int;
			
			for (i = 0; i < nrNoduri; i++)
			{
				noduri.push(new nod(i+1));
				this.addChild(noduri[i]);
				noduri[i].addEventListener(MouseEvent.DOUBLE_CLICK, atDoubleClick)
			}
			
			nrNoduriText.valid=false;
			adauga.valid=false;
			
			dfButon.valid=true;
			bfButon.valid=true;
			nodDf.valid=true;
		}
		
		private function getNodDf(e:MouseEvent):void
		{
			var nd:int;
			nd=parseInt(nodDf.text);
			
			if(0<nd&&nd<=nrNoduri)
				depthFirst(nd);
			else
			{
				if(nd==0)
					nodDf.text=">0";
				else
					nodDf.text="<="+nrNoduri;
				
				refaNoduri();
			}
		}
		
		private function getNodBf(e:MouseEvent):void
		{
			var nd:int;
			nd=parseInt(nodDf.text);
			
			if(0<nd&&nd<=nrNoduri)
				breadthFirst(nd);
			else
			{
				if(nd==0)
					nodDf.text=">0";
				else
					nodDf.text="<="+nrNoduri;
				
				refaNoduri();
			}
		}
		
		private function refaNoduri():void
		{
			var i:int;
			for(i=0;i<nrNoduri;i++)
				(noduri[i] as nod).refaNod();
		}
		
		private function getNrNoduri(e:MouseEvent):void
		{
			nrNoduri=parseInt(nrNoduriText.text);
			
			if(nrNoduri<=nrMaxim&&nrNoduri!=0)
				adaugaNoduri();
			else
				if(nrNoduri==0)
					nrNoduriText.text=">0";
			else
				nrNoduriText.text="<="+nrMaxim;
			
		}
		
		private function atDoubleClick(e:MouseEvent):void 
		{
			if (!flag)
			{
				n1 = e.target as nod;
				n1.deseneazaApasat();
			}
			else
			{
				n2 = e.target as nod;
				n1.deseneaza();
				addMuchie();
			}
			
			flag = !flag;
		}
		
		private function addMuchie():void
		{
			var muc : muchie = new muchie(n1, n2);
			addChildAt(muc, 0);
		}
		
		private function afiseazaLogo(e:MouseEvent):void
		{
			logg=new logo();
			logg.x=0;
			logg.y=0;
			this.addChild(logg);
		}
		
		var rezult : Array = new Array();
		var i : int = 0;
		var inter;
		
		private function depthFirst(nd:int):void
		{
			dfButon.valid=false;
			bfButon.valid=false;
			nodDf.valid=false;
			
			refaNoduri();
			
			var c : stack = new stack();
			var vecini : Array;
			
			c.push(noduri[nd-1]);
			(noduri[nd-1] as nod).utilizat=true;
			
			while(!c.isEmpty())
			{
				var curent : nod=c.top() as nod;
				c.pop();
				
				rezult.push(curent);
				
				vecini=curent.vecini();
				
				for each (var v:nod in vecini)
				{
					if(v.utilizat)
						continue;
					
					c.push(v);
					v.utilizat=true;
				}
			}
			inter = setInterval(afiseazaCuDelay,500);
		}
		
		private function breadthFirst(nd:int):void
		{
			dfButon.valid=false;
			bfButon.valid=false;
			nodDf.valid=false;
			
			refaNoduri();
			
			var c : queue = new queue();
			var vecini : Array;
			
			c.push(noduri[nd-1]);
			(noduri[nd-1] as nod).utilizat=true;
			
			while(!c.isEmpty())
			{
				var curent : nod=c.front() as nod;
				c.pop();
				
				rezult.push(curent);
				
				vecini=curent.vecini();
				
				for each (var v:nod in vecini)
				{
					if(v.utilizat)
						continue;
					
					c.push(v);
					v.utilizat=true;
				}
			}
			inter = setInterval(afiseazaCuDelay,500);
		}
		
		private function afiseazaCuDelay():void
		{
			if(i==rezult.length)
			{
				clearInterval(inter);
				
				rezult=[];
				i=0;
				
				dfButon.valid=true;
				bfButon.valid=true;
				nodDf.valid=true;
				
				return ;
			}
			(rezult[i] as nod).deseneazaDf();
			i++;
		}
	}	
}